package com.jack.cms.oss.core;

import javax.annotation.Resource;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jack.cms.config.properties.QiniuImageProperties;
import com.jack.cms.config.properties.QiniuProperties;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.fop.ExifRet;
import com.qiniu.api.fop.ImageExif;
import com.qiniu.api.fop.ImageInfo;
import com.qiniu.api.fop.ImageInfoRet;
import com.qiniu.api.fop.ImageView;
import com.qiniu.api.rs.GetPolicy;

@Service("qiniuImageUtils")
public class QiniuImageUtils extends QiniuUtils {
	private static Logger logger = LoggerFactory.getLogger(QiniuImageUtils.class);

	@Resource
	private QiniuImageProperties properties;

	public static String[] SUPPORT_IMAGE_SUFFIX = { ".jpg", ".jpeg", ".gif", ".png", ".bmp" };

	@Override
	public QiniuProperties getProperties() {
		return properties;
	}

	/**
	 * 查看图片属性
	 */
	public void getInfo(String key) {// key:"2-2.jpg"
		try {
			String type = properties.getQboxrsctlPrivate();
			if (type.equals("0")) {// 公开
				String url = buildUrl(getProperties().getDomain(), key);
				ImageInfoRet ret = ImageInfo.call(url);
				logger.info(JSON.toJSONString(ret));
			} else if (type.equals("1")) {// 私有空间
				Mac mac = getUploadMac();
				String baseUrl = buildUrl(properties.getDomain(), key);
				GetPolicy getPolicy = new GetPolicy();
				String url = getPolicy.makeRequest(baseUrl, mac);
				ImageInfoRet ret = ImageInfo.call(url);
				logger.info(JSON.toJSONString(ret));
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	/**
	 * 查看图片EXIF信息
	 */
	public void getEXIF(String key) {// key:"2-2.jpg";
		try {
			String type = properties.getQboxrsctlPrivate();
			if (type.equals("0")) {// 公开
				String url = buildUrl(getProperties().getDomain(), key);
				ExifRet ret = ImageExif.call(url);
				logger.info(JSON.toJSONString(ret));
			} else if (type.equals("1")) {// 私有空间
				Mac mac = getUploadMac();
				String baseUrl = buildUrl(properties.getDomain(), key);
				GetPolicy getPolicy = new GetPolicy();
				String url = getPolicy.makeRequest(baseUrl, mac);
				ExifRet ret = ImageExif.call(url);
				logger.info(JSON.toJSONString(ret));
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public String getUrl(String key) {
		return getUrl(key, null, null);
	}

	public String getUrl(String key, Integer w, Integer h) {// key:"2-2.jpg"
		String imageUrl = "";
		try {
			String type = properties.getQboxrsctlPrivate();
			if (type.equals("0")) {// 公开
				String url = buildUrl(getProperties().getDomain(), key);
				if (w == null && h == null) {
					imageUrl = url;
				} else {
					ImageView iv = new ImageView();
					iv.mode = 2;// 1表示，从中央裁剪为指定宽高
								// 2:限定目标缩略图的长和宽，将缩略图的大小限定在指定的宽高矩形内。
					iv.width = w;
					iv.height = h;
					// iv.quality = 1 ;
					// iv.format = "jpg" ;
					imageUrl = iv.makeRequest(url);
					// String str= iv.makeParams();
				}
			} else if (type.equals("1")) {// 私有
				// 其中domain是该bucket任意关联的一个domain，key是访问图片（不是缩略图）的键
				GetPolicy getPolicy = new GetPolicy();// 下载策略类
				Mac mac = getUploadMac();
				String baseUrl = buildUrl(properties.getDomain(), key);
				if (w == null && h == null) {
					imageUrl = getPolicy.makeRequest(baseUrl, mac);
				} else {
					ImageView iv = new ImageView();// 缩略图类
					iv.mode = 2;
					iv.width = w;
					iv.height = h;
					// iv.quality = 100 ;
					// iv.format = "jpg" ;
					String thumbUrl = iv.makeRequest(baseUrl);// 得到缩略图url（对于公开的bucket，到此为止就可以了）
					imageUrl = getPolicy.makeRequest(thumbUrl, mac);
				}
			}
		} catch (Exception e) {
			logger.error("", e);
		}
		return imageUrl;
	}

	/**
	 * 检查图片格式
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean checkImageSuffix(String fileName) {
		if (StringUtils.isNotBlank(fileName)) {
			String index = fileName.substring(fileName.lastIndexOf("."));
			index = StringUtils.lowerCase(index);
			if (ArrayUtils.contains(SUPPORT_IMAGE_SUFFIX, index)) {
				return true;
			}
		}
		return false;
	}

}
